﻿package KK.Controls.Tips {
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	public class Tip extends TBox {
		static public const BOX_WIDTH:int=76;
		static public const BOX_HEIGHT:int=50;
		static public const DIRECTION_WIDTH:int=16;
		static public const DIRECTION_HEIGHT:int=24;
		
		static public const DIRECTION_LEFT_DOWN:int=0;
		static public const DIRECTION_RIGHT_DOWN:int=1;
		static public const DIRECTION_LEFT_UP:int=2;
		static public const DIRECTION_RIGHT_UP:int=3;
		static public const DIRECTION_NONE:int=4;
		
		protected var directionList:Vector.<BitmapData>;
		
		protected var direction:int;
		 
		public function Tip() {
			// constructor code
			init();
		}
		private function init():void{
			this.width=BOX_WIDTH;
			this.height=BOX_HEIGHT;
			SetDirection();
		}
		
		/**
		 * 设置皮肤 
		 * @param bm
		 * 
		 */
		override public function SetBitmapData(bm:BitmapData,W_Lump:int,H_Lump:int):Vector.<BitmapData>{
			var result:Vector.<BitmapData>;
			directionList=new Vector.<BitmapData>;
			var boxbd:BitmapData=new BitmapData(W_Lump*3,H_Lump*3);
			var direction:BitmapData;
			var rect:Rectangle=new Rectangle(0,0,0,0);
			var point:Point=new Point(0,0);
			
			
			/*
				012
				345
				678
			*/
			//复制box数据
			//复制0
			rect.x=0;
			rect.y=DIRECTION_HEIGHT/2;
			rect.width=W_Lump;
			rect.height=H_Lump;
			
			boxbd.copyPixels(bm,rect,point);
			
			
			//复制3
			rect.y=H_Lump+DIRECTION_HEIGHT/2;
			point.y=H_Lump*1;
			boxbd.copyPixels(bm,rect,point);
			 
			//复制6
			point.y=H_Lump*2;
			rect.y=bm.height-DIRECTION_HEIGHT;
			boxbd.copyPixels(bm,rect,point);
			
			
			//复制1
			point.x=W_Lump;
			point.y=0;
			rect.x=bm.width-W_Lump*2;
			rect.y=DIRECTION_HEIGHT/2;
			rect.width=W_Lump;
			rect.height=H_Lump;
			
			boxbd.copyPixels(bm,rect,point);
			
			
			//复制3
			rect.y=H_Lump+DIRECTION_HEIGHT/2;
			point.y=H_Lump*1;
			boxbd.copyPixels(bm,rect,point);
			 
			//复制7
			point.y=H_Lump*2;
			rect.y=bm.height-DIRECTION_HEIGHT;
			boxbd.copyPixels(bm,rect,point);
			
			
			
			//复制2
			point.x=W_Lump*2;
			point.y=0;
			
			rect.x=bm.width-W_Lump;
			rect.y=DIRECTION_HEIGHT/2;
			rect.width=W_Lump;
			rect.height=H_Lump;
			
			boxbd.copyPixels(bm,rect,point);
			
			
			//复制5
			rect.y=H_Lump+DIRECTION_HEIGHT/2;
			point.y=H_Lump*1;
			boxbd.copyPixels(bm,rect,point);
			 
			//复制8
			point.y=H_Lump*2;
			rect.y=bm.height-DIRECTION_HEIGHT;
			boxbd.copyPixels(bm,rect,point);
			
			result=super.SetBitmapData(boxbd,W_Lump,H_Lump);
			
			//复制箭头数据
			
			
			
			
			
			//上左
			point.x=point.y=0;
			rect.y=0;
			rect.x=W_Lump;
			rect.width=DIRECTION_WIDTH;
			rect.height=DIRECTION_HEIGHT;
			
			direction=new BitmapData(DIRECTION_WIDTH,DIRECTION_HEIGHT);
			direction.copyPixels(bm,rect,point);
			
			
			//上右
			directionList.push(direction);
			
			rect.x=W_Lump+DIRECTION_WIDTH;
			direction=new BitmapData(DIRECTION_WIDTH,DIRECTION_HEIGHT);
			direction.copyPixels(bm,rect,point);
			
			directionList.push(direction);
			
			//下左
			rect.x=W_Lump;
			rect.y=bm.height-DIRECTION_HEIGHT;
			direction=new BitmapData(DIRECTION_WIDTH,DIRECTION_HEIGHT);
			direction.copyPixels(bm,rect,point);
			
			directionList.push(direction);
			
			//下右
			rect.x=W_Lump+DIRECTION_WIDTH;
			rect.y=bm.height-DIRECTION_HEIGHT;
			direction=new BitmapData(DIRECTION_WIDTH,DIRECTION_HEIGHT);
			direction.copyPixels(bm,rect,point);
			
			directionList.push(direction);
			
			return result;
			
		}
		
		/**
		 * 重绘制 
		 * @param width
		 * @param height
		 * 
		 */
		override public function ReDraw():void{
			//除开箭头的高度 
			
			if(width!=0&&height!=0&&directionList!=null&&directionList.length>0)
			{
				height=Lump.y*int(height/Lump.y);
				width=Lump.x* int(width/Lump.x);
				super.ReDraw( ); 
				
				
				//重绘箭头
				var bd:BitmapData=new BitmapData(width,height,true,0);
				var rect:Rectangle=new Rectangle();
				var point:Point=new Point();
				rect.x=rect.y=0
				rect.width=DIRECTION_WIDTH;
				rect.height=DIRECTION_HEIGHT;
				
				switch(direction)
				{
					case DIRECTION_LEFT_UP:
						point.y=0;
						point.x=Lump.x;
						bd.copyPixels(this.bitmapData,new Rectangle(0,0,width,height),new Point(0,DIRECTION_HEIGHT-Lump.y));
						bd.copyPixels(directionList[0],rect,point);
						
					break;
					case DIRECTION_RIGHT_UP:
						point.x=width-DIRECTION_WIDTH-Lump.x;
						point.y=0;
						bd.copyPixels(this.bitmapData,new Rectangle(0,0,width,height),new Point(0,DIRECTION_HEIGHT-Lump.y));
						bd.copyPixels(directionList[1],rect,point);
						
					break;
					case DIRECTION_LEFT_DOWN:
						point.x=Lump.x;
						point.y=height-DIRECTION_HEIGHT;
						bd.copyPixels(this.bitmapData,new Rectangle(0,0,width,height),new Point(0,0));
						bd.copyPixels(directionList[2],rect,point);
					break;
					case DIRECTION_RIGHT_DOWN:
						point.x=width-DIRECTION_WIDTH-Lump.x;
						point.y=height-DIRECTION_HEIGHT;
						bd.copyPixels(this.bitmapData,new Rectangle(0,0,width,height),new Point(0,0));
						bd.copyPixels(directionList[3],rect,point);
					break;
					case DIRECTION_NONE:
						bd.copyPixels(this.bitmapData,new Rectangle(0,0,width,height),new Point(0,0));
						break;
				}
				
				this.bitmapData=bd;
				
			}
			
			
		}
		public function GetDirection():int{
			return this.direction;
			
		}
		public function SetDirection(direction:int=DIRECTION_RIGHT_DOWN):void{
			this.direction=direction;
			ReDraw();
		}
		
		override public function set width(value:Number):void{
			if(value<Lump.x*2+DIRECTION_WIDTH)
			{
				super.width=Lump.x*2+DIRECTION_WIDTH;
			}else{
				super.width=value;
			}
		}
		override public function set height(value:Number):void{
			if(value<Lump.y+DIRECTION_HEIGHT)
			{
				super.height=Lump.y+DIRECTION_HEIGHT;
			}else{
				super.height=value;
			}
		} 
		
	}
	
}
